<?php
	class user extends spModel{
		var $pk = "id"; // 每个学年唯一的标志，可以称为主键
		var $table = "student"; // 数据表的名称
	
	    var $validator = array(
			"rules" => array( // 规则
				'account' => array(  // 这里是对username的验证规则
					'required' => true, // username不能为空
					'minlength' => 1,  // username长度不能小于5
					'maxlength' => 50, // username长度不能大于15
				),	
				'password' => array(
					'required' => true, // username不能为空
				),	
				
			),
			"messages" => array( // 提示信息
				'account' => array(
					'required' => '账号不能为空',
					'minlength' => '账号不能少于1个字符',
					'maxlength' => '账号不能大于50字符',
				),
				'password' => array(
					'required' => '密码不能为空'
				),
			)
		);

		
		public function user_login($uname, $password){
			if($result = $this->spValidator(array("account"=>$uname, "password"=>$password))) {
                foreach($result as $item)
                    throw new Exception($item[0]);
            }
            $condition = "account = '".$uname."'";
            if(!$this->find($condition))
                throw new Exception("账号输入有误，请重新输入！");
			$condition .= 'and password = '. md5($password);
			if(!$student_rs = $this->find($condition))
				throw new Exception("密码输入有误，请重新输入！");    

			if($student_rs["status"] == '1'){
				$this->save_login_cookie(1, $uname, $password);
				if(substr(strtolower($student_rs["imgheader"]), 0, 4) != "http")
					$student_rs["imgheader"] = WEB_ROOT . "/" . $user_rs["imgheader"];
				$_SESSION["sswcdc_user"] = $student_rs;
			}elseif($student_rs["status"] == '0'){
				throw new Exception("该账号未激活，无法登录");
			}else{
				throw new Exception("该账号已屏蔽，无法登录");
			}
		}
		
		public function save_login_cookie($type, $username, $password){
			import("Common.php");
			$cookie_array = array("t"=>$type, "u"=>$username, "p"=>Common::authcode($password));
			setcookie("sswcdc_user", serialize($cookie_array), time() + 3600 * 24 * 365, COOKIE_PATH);
		}
		
		public function clear_login_cookie(){
			setcookie("sswcdc_user", null, time() - 1, COOKIE_PATH);
		}
		
		public function reset_pwd($tel){
			import("Common.php");
			$key = Common::randomNum(6);
			$this->update(array("regtel"=>$tel), array("password"=>md5($key)));
			return $key;
		}
		
		public function auto_login(){
			import("Common.php");
			if($_COOKIE["sswcdc_user"] && !$_SESSION["sswcdc_user"]){
				$userdata = unserialize($_COOKIE["sswcdc_user"]);
				/*
				print_r($userdata);
				exit();
				*/
				switch($userdata["t"]){
					case "2":
						if($user_rs = $this->find(array("typeid"=>2, "openid"=>$userdata["u"]))){
							if(substr(strtolower($user_rs["imgheader"]), 0, 4) != "http")
								$user_rs["imgheader"] = WEB_ROOT . "/" . $user_rs["imgheader"];
							$_SESSION["sswcdc_user"] = $user_rs;
						}
					break;
					case "3":
						if($user_rs = $this->find(array("typeid"=>3, "openid"=>$userdata["u"]))){
							if(substr(strtolower($user_rs["imgheader"]), 0, 4) != "http")
								$user_rs["imgheader"] = WEB_ROOT . "/" . $user_rs["imgheader"];
							$_SESSION["sswcdc_user"] = $user_rs;
						}
					break;
					default:
						$this->user_login($userdata["u"], Common::authcode($userdata["p"], "DECODE"));
					break;
				}
			}
		}
		
		public function getuppath($id, $mark = ""){
			return $mark ? "uploads/userup/$id/$mark/" : "uploads/userup/$id/";
		}
		
		public function checkupfile($mark, $type = array("gif", "png", "jpg", "jpeg", "png")){
			if (!empty($_FILES[$mark]['name'])){
				$filename = basename($_FILES[$mark]['name']);       //文件名
				$file_ext = strtolower(substr($filename, strrpos($filename, '.') + 1));
				if(!in_array($file_ext, $type))
					throw new Exception('您选择的文件类型不正确'.$file_ext);
			}
		}
		
		public function upfile($mark, $id, $upname, $field = "imgheader", $thumb = array("255", "255"), $type = array("gif", "png", "jpg", "jpeg", "png")){
			if (!empty($_FILES[$mark]['name']) && $id){
				import("Common.php");
				import("Image.php");
				$flag = true;
				$userfile_name = $_FILES[$mark]['name'];            //上传图片的全路径
				$userfile_tmp  = $_FILES[$mark]['tmp_name'];        //临时文件的全路径
				$userfile_size = $_FILES[$mark]['size'];            //上传文件的大小
				$filename = basename($_FILES[$mark]['name']);       //文件名
				$file_ext = strtolower(substr($filename, strrpos($filename, '.') + 1));
				$this->checkupfile($mark, $type);
				$pic_name = $upname ? $upname : Common::randomkeys(16);
				$dir_str = $this->getuppath($id, $mark);
				$file_dir = $dir_str;
				if(!is_dir($file_dir))
					Common::rmkdir($file_dir);
				$image_location = $file_dir . $pic_name . '.' . $file_ext;
				if(!move_uploaded_file($_FILES[$mark]['tmp_name'], $image_location))
					throw new Exception('未知错误，上传失败');
				if($thumb[0] && $thumb[1]){
					$image = new Image();
					$image->open($image_location);
					$thumb_image_location = $file_dir . $pic_name . '.' .$file_ext;
					$thumb_width = intval($thumb[0]);
					$thumb_height = intval($thumb[1]);
					$image->thumbImage($thumb_image_location, $thumb_width, $thumb_height);
				}
				if(is_array($field)){
					$fieldstr = implode(",", $field);
					$data["data"][$field[0]] = $dir_str;
					$data["data"][$field[1]] = $pic_name . '.' .$file_ext;
					$rs = $this->find(array("id"=>$id), null, $fieldstr);
					$data["oralfile"][] = implode(null, $rs);
				}else{
					$data["data"][$field] = $dir_str . $pic_name . '.' .$file_ext;
					$rs = $this->find(array("id"=>$id), null, $field);
					$data["oralfile"][] = $rs[$field];
				}
				$data["source"]['realpath'][] = $file_dir . '.' .$data['filename'];
				return $data;
			}
		}
	}
?>